﻿using EntityModel.CommodityMent;
using EntityModel.Helper;
using EntityModel.PageListInfor;
using IServices.IGoodsConfig;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using IServices.ICurrency;
using System.Data;

namespace Services.GoodsCats
{
    /// <summary>
    /// 商品分类实现
    /// </summary>
    public class GoodsCats : IGoodsCats
    {
        private readonly RoutineDbContext _dbContext;
        private readonly ICurrencyServer _currencyServer;

        public GoodsCats(RoutineDbContext dbContext, ICurrencyServer currencyServer)
        {
            _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
            _currencyServer = currencyServer ?? throw new ArgumentNullException(nameof(currencyServer));
        }

        /// <summary>
        /// 添加商品分类信息
        /// </summary>
        /// <param name="goods_Cats">商品分类信息</param>
        /// <returns></returns>
        public  Task<int> AddGoodsCatsInforAsync(AikeApplet_goods_cats goods_Cats)
        {
            if (goods_Cats == null) throw new ArgumentNullException(nameof(goods_Cats));
            var result = _currencyServer.ExecuteSqlAsync(null, $"select catName from goods_cats where catName ='{goods_Cats.catName}'");
            if (result.Rows.Count > 0) throw new ArgumentNullException("已存在相同的商品分类名称");
            _dbContext._Goods_Cats.Add(new AikeApplet_goods_cats
            {
                parentId = goods_Cats.parentId == 0 ? 1 : goods_Cats.parentId,
                catName = goods_Cats.catName,
                createTime = DateTime.Now,
                commissionRate = goods_Cats.commissionRate,
                catImg = goods_Cats.catImg,
                subTitle = goods_Cats.subTitle,
                simpleName = goods_Cats.simpleName,
                seoTitle = goods_Cats.seoTitle,
                seoKeywords = goods_Cats.seoKeywords,
                seoDes = goods_Cats.seoDes,
                catListTheme = goods_Cats.catListTheme,
                detailTheme = goods_Cats.detailTheme,
                mobileCatListTheme = goods_Cats.mobileCatListTheme,
                wechatCatListTheme = goods_Cats.wechatCatListTheme,
                wechatDetailTheme = goods_Cats.wechatDetailTheme
            });
            return  _dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 删除商品分类信息
        /// </summary>
        /// <param name="catId">商品分类信息</param>
        /// <returns></returns>
        public async Task<bool> DeletedGoodsCatsInforAsync(AikeApplet_goods_cats catId)
        {
            if (catId.catId <= 0) throw new ArgumentNullException(nameof(catId.catId));

            _dbContext._Goods_Cats.Remove(catId);

            return await _dbContext.SaveChangesAsync() >= 0;
        }

        /// <summary>
        /// 获取商品分类信息
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<AikeApplet_goods_cats>> GetAllGoodsCatsAsync()
        {
            return (IEnumerable<AikeApplet_goods_cats>)await _dbContext._Goods_Scores.AsQueryable().ToListAsync();
        }

        /// <summary>
        /// 通过商品分类id获取商品分类数据
        /// </summary>
        /// <param name="catId">商品分类id</param>
        /// <returns></returns>
        public async Task<AikeApplet_goods_cats> GetGoodsCatsDataByCatsidAsync(int catId)
        {
            if (catId <= 0) throw new ArgumentNullException(nameof(catId));
            return await _dbContext._Goods_Cats.FirstOrDefaultAsync(x => x.catId == catId);
        }

        /// <summary>
        /// 获取商品分类信息（分页）
        /// </summary>
        /// <param name="goods_Cats">商品分类信息</param>
        /// <returns></returns>
        public async Task<DbQueryResult> GetGoodsCatsPageListAsync(AikeApplet_goods_cats goods_Cats)
        {
            if (goods_Cats == null) throw new ArgumentNullException(nameof(goods_Cats));
            return await _currencyServer.PagingByRownumber("goods_cats", "catId", goods_Cats.PageIndex, goods_Cats.PageSize
               , goods_Cats.SortBy, goods_Cats.SortOrder, "", true, "*");
        }

        /// <summary>
        /// 通过sql获取商品分类名称
        /// </summary>
        /// <returns></returns>
        public async Task<DataTable> GetParentInfomationBySql()
        {
            var result = _currencyServer.ExecuteSqlAsync(null, "select parentId,catName from goods_cats");
            if (result.Rows.Count == 0) return null;
            return result;
        }

        /// <summary>
        /// 修改商品分类信息
        /// </summary>
        /// <param name="goods_Cats">商品分类信息</param>
        /// <returns></returns>
        public async Task<bool> UpdateGoodsCatsInforAsync(AikeApplet_goods_cats goods_Cats)
        {
            if (goods_Cats.catId <= 0) throw new ArgumentNullException(nameof(goods_Cats.catId));
            var data = await GetGoodsCatsDataByCatsidAsync(goods_Cats.catId);
            if (data == null) throw new ArgumentNullException(nameof(data));
            data.parentId = goods_Cats.parentId;
            data.catName = goods_Cats.catName;
            data.createTime = DateTime.Now;
            data.commissionRate = goods_Cats.commissionRate;
            data.catImg = goods_Cats.catImg;
            data.subTitle = goods_Cats.subTitle;
            data.simpleName = goods_Cats.simpleName;
            data.seoTitle = goods_Cats.seoTitle;
            data.seoKeywords = goods_Cats.seoKeywords;
            data.seoDes = goods_Cats.seoDes;
            data.catListTheme = goods_Cats.catListTheme;
            data.detailTheme = goods_Cats.detailTheme;
            data.mobileCatListTheme = goods_Cats.mobileCatListTheme;
            data.wechatCatListTheme = goods_Cats.wechatCatListTheme;
            data.wechatDetailTheme = goods_Cats.wechatDetailTheme;
            _dbContext._Goods_Cats.Update(data);
            return await _dbContext.SaveChangesAsync() >= 0;
        }
    }
}
